﻿import QtQml 2.3
import QtQuick 2.15
import QtQuick.Controls 2.15

Item{

    property string number: ""
    property string name: ""
    property string ip: ""
    property string port: ""
    property string img: ""

    Component.onCompleted: {
        //加载添加联系人界面组件
        addPersonLoader.source = "AddPerson.qml"

        //获取数据库用户信息
        var listResult = dbManager.queryUserInfo("UserInfo")
        if(listResult.length){
            number = listResult[0].number
            name = listResult[0].name
            ip = listResult[0].ip
            port = listResult[0].port
            img = listResult[0].img
        }

    }

    Rectangle{
        id: pageBackRect
        border.color: "#000000"
        anchors.fill: parent

        Rectangle{
            id: header
            width: parent.width
            height: width/10
            anchors.top: parent.top
            color: "#404142"
            z: 10


            MouseArea{
                anchors.fill: parent
            }

            Rectangle{
                width: parent.width/4
                height: parent.height
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                opacity: 0
                MouseArea{
                    anchors.fill: parent
                    property point clickPos:"0,0"
                    onPressed:{
                    clickPos =Qt.point(mouseX, mouseY)
                    }
                    onPositionChanged:{
                    //鼠标偏移量
                    var delta =Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
                    rootWindow.setX(rootWindow.x+delta.x)
                    rootWindow.setY(rootWindow.y+delta.y)
                    }
                }
            }

            Text {
                id: appTitle
                color: "#ffffff"
                text: qsTr("在线聊天")
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                font.family: kuaikanshijietiFont.font.family
                font.styleName: "Bold"
                font.pointSize: 14

            }

            Image {
                id: userPortrait
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                width: parent.height/2
                height: width
                anchors.leftMargin: width/2
                source: img?img:"qrc:/img/userPortrait.png"

                Component.onCompleted:{
                    var x = userPortrait.x + userPortrait.width
                    var y = userPortrait.y + userPortrait.height
                    const component = Qt.createComponent("UserInfoRect.qml")
                    var data ={ x: x, y: y, "number": number, "name": name, "ip": ip, "port": port}
                    component.createObject(pageBackRect, data)
                 }

                MouseArea{
                    anchors.fill: parent
                    hoverEnabled: true

                    onEntered: {
                        userPortrait.scale = 1.2
                        cursorShape = Qt.PointingHandCursor
                        pageBackRect.children[pageBackRect.children.length-1].visible = true

                    }
                    onExited: {
                        userPortrait.scale = 1
                        cursorShape = Qt.ArrowCursor
                        pageBackRect.children[pageBackRect.children.length-1].visible = false
                    }

                }

            }

//            Text {
//                id: userName
//                color: "#ffffff"
//                text: qsTr("用户名")
//                anchors.verticalCenter: parent.verticalCenter
//                anchors.left: userPortrait.right
//                font.pointSize: 8
//                font.family: kuaikanshijietiFont.font.family
//                anchors.leftMargin: userPortrait.width

//            }

            Button {
                id: settingButton
                width: parent.height/2
                height: width
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                icon.color: "#00000000"
                icon.source: "qrc:/img/setting.png"
                anchors.rightMargin: width

                onClicked: {
                    pageStackView.push("SettingPage.qml")
                    console.log("SettingPage.qml is loading...")
                }

            }
        }

        Dialog {
            id: dialogExit
            title: "确认退出吗？"
            width: parent.width/2
            height: width/2
            topMargin: parent.height/2
            leftMargin: width/2
            standardButtons: Dialog.Ok | Dialog.Cancel

            onAccepted: {
                Qt.quit()
                console.log("App Exit...")
            }
            onRejected: console.log("Cancel clicked")
        }

        Rectangle{
            id: personList
            width: parent.width
            height: parent.height - header.height - bottomBar.height
            anchors.top: header.bottom
            color: "#ffffff"
            z: 1

            Flickable {
                anchors.fill: parent
                contentWidth: width
                contentHeight: personListColumn.height
                boundsBehavior: Flickable.DragOverBounds
                flickableDirection: Flickable.VerticalFlick

                ScrollBar.vertical: ScrollBar{
                    width: 5
                    policy: ScrollBar.AsNeeded
                }

                Column{
                    id: personListColumn
                    width: parent.width
                    spacing: 5

                    Component.onCompleted:{
                        const component = Qt.createComponent("PersonBarComponent.qml")
                        var listResult = dbManager.queryDistinctPerson("TotalPerson")


                        for(var i in listResult){
                            var number = listResult[i].number
                            var name = listResult[i].name
                            var img = listResult[i].img
                            var ip = listResult[i].ip
                            var port = listResult[i].port
                            component.createObject(personListColumn);
                            var listNewMsg = dbManager.queryNewMsg("TotalMsg", number)
                            personListColumn.children[i].children[2].text = name
                            personListColumn.children[i].number = number
                            personListColumn.children[i].name = name
                            personListColumn.children[i].curruntIP = ip
                            personListColumn.children[i].curruntPort = port
                            if(listNewMsg.length){
                                console.log("账号：" + number + " 名称：" + name + " 最新消息：" + listNewMsg)
                                personListColumn.children[i].children[3].text = listNewMsg[0].msg
                                personListColumn.children[i].children[4].text = listNewMsg[0].time

                            }

                            if(img){
                                personListColumn.children[i].children[1].source = listResult[i].img
                            }
                        }

                    }
                }

            }



        }

        Rectangle{
            id: bottomBar
            width: parent.width
            height: width/12
            anchors.top: personList.bottom
            color: "#404142"
            z: 10

            MouseArea{
                anchors.fill: parent
            }

            Button {
                id: exitButton
                ToolTip.visible: hovered
                ToolTip.text: qsTr("退出程序")
                width: parent.height*0.6
                height: width
                anchors.verticalCenter: parent.verticalCenter
                icon.color: "#00000000"
                anchors.right: parent.right
                icon.source: "qrc:/img/exit.png"
                anchors.rightMargin: width

                onClicked: {
                    dialogExit.open()
                }

            }

            Button {
                id: miniButton
                ToolTip.visible: hovered
                ToolTip.text: qsTr("隐藏窗口")
                width: parent.height*0.6
                height: width
                anchors.verticalCenter: parent.verticalCenter
                icon.color: "#00000000"
                anchors.right: exitButton.left
                anchors.rightMargin: width
                icon.source: "qrc:/img/mini.png"

                onClicked: {
                    hide()
                    console.log("App mini...")
                }

            }

            Button {
                id: addPersonButton
                ToolTip.visible: hovered
                ToolTip.text: qsTr("添加用户")
                width: parent.height*0.6
                height: width
                anchors.verticalCenter: parent.verticalCenter
                icon.color: "#00000000"
                anchors.right: miniButton.left
                anchors.rightMargin: width
                icon.source: "qrc:/img/addperson.png"

                onClicked: {
                    addPersonLoader.children[0].visible = true
                    console.log("addPersonButton is clicked...")

                }

            }

            Button {
                id: loginButton
                ToolTip.visible: hovered
                ToolTip.text: qsTr("本地登录")
                width: parent.height*0.6
                height: width
                anchors.verticalCenter: parent.verticalCenter
                icon.color: "#00000000"
                anchors.right: addPersonButton.left
                anchors.rightMargin: width
                icon.source: "qrc:/img/login.png"


                onClicked: {
                    pageStackView.push("SettingPage.qml")

                }

            }

            Button {
                id: flushButton
                ToolTip.visible: hovered
                ToolTip.text: qsTr("手动刷新")
                width: parent.height*0.6
                height: width
                anchors.verticalCenter: parent.verticalCenter
                icon.color: "#00000000"
                anchors.right: loginButton.left
                anchors.rightMargin: width
                icon.source: "qrc:/img/flush.png"


                onClicked: {
                    //刷新页面
                    pageStackView.clear(StackView.Immediate)
                    pageStackView.push("FirstPage.qml", StackView.Immediate)

                    //重新监听端口
                    var port = inputChangePort.text
                    dbManager.updatePort("UserInfo", port)
                    socketManager.initNetwork(port)

                }

            }
        }

        Loader{
            id: addPersonLoader
            anchors.fill: parent
            z: 2
        }
    }

}

/*##^##
Designer {
    D{i:0;autoSize:true;height:480;width:640}
}
##^##*/
